package com.xgzit.xgzmall.order.controller;

import com.xgzit.xgzmall.order.entity.OrderEntity;
import com.xgzit.xgzmall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @Author xgz
 * @Date 2021-06-23 21:29
 */
@Slf4j
@RequestMapping("/rabbit")
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMessageTest(@RequestParam(value = "num",defaultValue = "10") Integer num){

        for (int i = 0; i < num; i++) {
            if (i % 2 == 0){
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("啦啦啦:"+i);
                reasonEntity.setStatus(i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java", reasonEntity, new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送完成：{}", reasonEntity);
            }else{
                OrderEntity order = new OrderEntity();
                order.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java-exchange","hello1.java", order, new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送完成：{}", order);
            }
        }
        return "ok";
    }
}
